package com.hjw.weixun.dao.impl;

import java.util.Date;
import java.util.List;

import com.hjw.weixun.Template.JdbcTemplate;
import com.hjw.weixun.dao.MessageDao;
import com.hjw.weixun.domain.Message;
import com.hjw.weixun.domain.Weixun;

public class MessageDaoJdbcImpl extends JdbcTemplate<Message> implements
		MessageDao {
	public static MessageDao messageDao = null;

	// 单例
	public static MessageDao getInstance() {
		if (messageDao == null) {
			messageDao = new MessageDaoJdbcImpl();
		}
		return messageDao;
	} 

	@Override
	public int addMessage(Message message) {
		String content = message.getContent();
		Date publishTime = message.getPublishtime();

		Integer weixunid = null == message.getWeixun() ? 0 : message
				.getWeixun().getId();

		Integer userid = null == message.getUser() ? 0 : message.getUser()
				.getUserid();
		Integer pid = message.getPid();

		String sql = "insert into message(content,publishtime,weixunid,userid,pid) values (?,?,?,?,?) ";
		Object[] args = new Object[] { content, publishTime, weixunid, userid,
				pid };
		return this.execute(sql, args);

	}

	@Override
	public Message getMessage(Message message) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Message> getMessageList(Weixun weixun) {
		String sql = "SELECT * FROM message WHERE weixunid = ? ORDER BY publishtime DESC";
		Object[] args = new Object[] { weixun.getId() };

		return this.executeQueryAllToList(sql, args);
	}

}
